$(document).ready(function(){
	$('#add_province').click(function(){
		addProvince();		
	});
	
	$('#add_district').click(function(){
		addDistrict();		
	});
	
	$('#add_ward').click(function(){
		addWard();		
	});
	
	$('.nav_bg a').click(function() {
		$('.nav_bg a').removeClass('active');
		$(this).addClass('active');
	});
	
	$('select#select_province1').change(function() {
		selected = $('select#select_province1').val();
		if(selected == 'empty'){
			$('table#provinceTable tbody').html('');
			return false;
		}
		getAllDistricts(selected);
	});
	
	$('select#select_province2').change(function() {
		selected = $('select#select_province2').val();
		if(selected == 'empty'){
			$('select#select_district').html("");
			$('select#select_district').append("<option id='empty' value='empty'>--" + _("Select district") + "--</option>");
			$('table#wardTable tbody').html('');
			return false;
		}
		getDistrictOptions(selected);
	});
	
	$('select#select_district').change(function() {
		selected = $('select#select_district').val();
		if(selected == 'empty'){
			$('table#wardTable tbody').html('');
			return false;
		}
		getAllWards(selected);
	});
		
});

function showOneDiv(divId) {
	if (divId == 'province') {
		$('#district').hide("slow");
		$('#ward').hide("slow");
		$('#error_province').html('');
		$('#province_name').val('');
		getAllProvinces();
		$('#province').show("slow");
	}else if (divId == 'district') {
		$('#province').hide("slow");
		$('#ward').hide("slow");
		$('#district_name').val('');
		$('table#districtTable tbody').html('');
		getAllProvincesSelect(divId);
		$('#district').show("slow");
	}else{
		$('#province').hide("slow");
		$('#district').hide("slow");
		$('#ward_name').val('');
		$('#abbr_code').val('');
		$('table#wardTable tbody').html('');
		getAllProvincesSelect(divId);
		$('#ward').show("slow");
	}
};

function getAllProvinces(){
	$('#error_province').html('');
	$.get(
		'../Province/getAllProvinces?set_ajax_view',
		{
		},
		function(data){
			$('table#provinceTable tbody').html('');
			$('table#provinceTable tbody').append(data);
			styleTable($('table#provinceTable')); 
		}
	);
};



function addProvince(){
	$('#error_province').text('');
	var provinceName = $.trim($('#province_name').val());
	if (provinceName == '') {
		$('#error_province').text(_("Please input province"));
	} else {
		$.post(
			'../Province/addProvince?set_ajax_view',
			{
			name: $('#province_name').val()
			},
			function(data){
				if(data == 1){
					$('#error_province').text(_("Province existed"));
					return false;
				}
				$('table#provinceTable tbody').html('');
				$('table#provinceTable tbody').append(data);
				$('#province_name').val('');
				styleTable($('table#provinceTable')); 
			}
		);
	}
};


function editProvince(provinceId){
	$('#error_province').html('');
	$('#province_' + provinceId + '_name_input').val($('#province_' + provinceId + '_name_span').html());
	$('#province_' + provinceId + '_name_span').hide();
	$('#province_' + provinceId + '_edit').hide();
	$('#province_' + provinceId + '_name_input').show();
	$('#province_' + provinceId + '_save').show();
	$('#province_' + provinceId + '_cancel').show();
}


function saveProvince(provinceId){
	$('#error_province').html('');
	var provinceName = $('#province_' + provinceId + '_name_input').val();
	if (provinceName.split(' ').join('') == '') {
		$('#error_province').html("<font color='red'>" + _("Please input province") + "</font>");
	} else {
		$.post(
			'../Province/saveProvince?set_ajax_view',
			{
				provinceId: provinceId,
				name: $('#province_' + provinceId + '_name_input').val()
			},
			function(data){
				if(data == 1){
					$('#error_province').html("<font color='red'>" + _("Province existed") + "</font>");
					return false;
				}
				$('#province_' + provinceId + '_name_span').html(data);
				$('#province_' + provinceId + '_name_span').show();
				$('#province_' + provinceId + '_edit').show();
				$('#province_' + provinceId + '_name_input').hide();
				$('#province_' + provinceId + '_save').hide();
				$('#province_' + provinceId + '_cancel').hide();			
			}
		);
	}
};

function cancelProvince(provinceId){
	$('#province_' + provinceId + '_name_span').show();
	$('#province_' + provinceId + '_edit').show();
	$('#province_' + provinceId + '_name_input').hide();
	$('#province_' + provinceId + '_save').hide();
	$('#province_' + provinceId + '_cancel').hide();	
	$('#error_province').html('');
}


// district's functions
function getAllDistricts(provinceId){
	$('#error_district').html('');
	$.get(
		'../Province/getAllDistricts?set_ajax_view',
		{provinceId: provinceId
		},
		function(data){
			$('table#districtTable tbody').html('');
			$('table#districtTable tbody').append(data);
			styleTable($('table#districtTable')); 
		}
	);
};



function addDistrict(){
	$('#error_district').text('');
	var districtName = $.trim($('#district_name').val());
	var provinceId = $('select#select_province1').val();
	if (districtName == '' ) {
		$('#error_district').text(_("Please input district"));
		return false;
	}else if( provinceId == 'empty' ){
		$('#error_district').text(_("Please select province"));
		return false;
	} else {
		$.post(
			'../Province/addDistrict?set_ajax_view',
			{
			name: $('#district_name').val(),
			provinceId: provinceId
			},
			function(data){
				if(data == 1){
					$('#error_district').text(_("District existed"));
					return false;
				}
				$('table#districtTable tbody').html('');
				$('table#districtTable tbody').append(data);
				$('#district_name').val('');
				styleTable($('table#districtTable')); 
				
			}
		);
	}
};


function editDistrict(districtId){
	$('#error_district').html('');
	$('#district_' + districtId + '_name_input').val($('#district_' + districtId + '_name_span').html());
	$('#district_' + districtId + '_name_span').hide();
	$('#district_' + districtId + '_edit').hide();
	$('#district_' + districtId + '_name_input').show();
	$('#district_' + districtId + '_save').show();
	$('#district_' + districtId + '_cancel').show();
}


function saveDistrict(districtId){
	$('#error_district').html('');
	var districtName = $('#district_' + districtId + '_name_input').val();
	if (districtName.split(' ').join('') == '') {
		$('#error_district').html("<font color='red'>" + _("Please input district") + "</font>");
	} else {
		$.post(
			'../Province/saveDistrict?set_ajax_view',
			{
				districtId: districtId,
				provinceId: $('select#select_province1').val(),
				name: $('#district_' + districtId + '_name_input').val()
			},
			function(data){
				if(data == 1){
					$('#error_district').html("<font color='red'>" + _("District existed") + "</font>");
					return false;
				}
				$('#district_' + districtId + '_name_span').html(data);
				$('#district_' + districtId + '_name_span').show();
				$('#district_' + districtId + '_edit').show();
				$('#district_' + districtId + '_name_input').hide();
				$('#district_' + districtId + '_save').hide();
				$('#district_' + districtId + '_cancel').hide();			
			}
		);
	}
};
function cancelDistrict(districtId){
	$('#district_' + districtId + '_name_span').show();
	$('#district_' + districtId + '_edit').show();
	$('#district_' + districtId + '_name_input').hide();
	$('#district_' + districtId + '_save').hide();
	$('#district_' + districtId + '_cancel').hide();	
	$('#error_district').html('');
}

// ward's functions
function getDistrictOptions(provinceId){
	$('#error_ward').html('');
	$.get(
		'../Province/getAllDistrictsSelect?set_ajax_view',
		{provinceId: provinceId
		},
		function(data){
			$('select#select_district').html('');
			$('select#select_district').append(data);
			districtId = $('select#select_district').val();
			getAllWards(districtId);
		}
	);
	
};

function getAllWards(districtId){
	$('#error_ward').html('');
	if(districtId == 'empty' ){
		return false;
	}
	$.get(
		'../Province/getAllWards?set_ajax_view',
		{districtId: districtId
		},
		function(data){
			$('table#wardTable tbody').html('');
			$('table#wardTable tbody').append(data);
			styleTable($('table#wardTable')); 
		}
	);
};



function addWard(){
	$('#error_ward').html('');
	var wardName = $.trim($('#ward_name').val());
	var code = $.trim($('#abbr_code').val());
	var districtId = $('select#select_district').val();
	if (wardName == '' ) {
		$('#error_ward').text(_("Please input ward"));
		return false;
	} else if(code == ''){
		$('#error_ward').text(_("Please input abbreviation code"));
		return false;
	} else if(districtId == 'empty'){
		$('#error_ward').text(_("Please select province and district"));
		return false;
	} else {		
		$.ajax({
			type:	'POST',
			url:	'../Province/addWard?set_ajax_view',
			data:	'name=' + wardName +
					'&districtId=' + districtId +
					'&code=' + code,
			success: function(data) {
				if(data == 1){
					$('#error_ward').text(_("Ward existed"));
					return false;
				}				
				$('table#wardTable tbody').html('');
				$('table#wardTable tbody').append(data);
				$('#ward_name').val('');
				$('#abbr_code').val('');
				
				styleTable($('table#wardTable'));
			}
		});	
		
	}				
};


function editWard(wardId){
	$('#error_ward').html('');
	$('#ward_' + wardId + '_name_input').val($('#ward_' + wardId + '_name_span').text()).show();
	$('#ward_' + wardId + '_name_span').hide();		
	
	$('#ward_' + wardId + '_code_input').val($('#ward_' + wardId + '_code_span').text()).show();
	$('#ward_' + wardId + '_code_span').hide();	
		
	$('#ward_' + wardId + '_edit').hide();
	$('#ward_' + wardId + '_save').show();
	$('#ward_' + wardId + '_cancel').show();
}


function saveWard(wardId){
	$('#error_ward').html('');
	var wardName = $('#ward_' + wardId + '_name_input').val();
	if (wardName.split(' ').join('') == '') {
		$('#error_ward').html("<font color='red'>" + _("Please input ward") + "</font>");
	} else {
		$.post(
			'../Province/saveWard?set_ajax_view',
			{
				wardId: wardId,
				districtId: $('select#select_district').val(),
				name: $('#ward_' + wardId + '_name_input').val(),
				code: $('#ward_' + wardId + '_code_input').val(),
			},
			function(data){
				if (data == 1){
					$('#error_ward').html("<font color='red'>" + _("Ward existed") + "</font>");
					return false;
				}				
				$('#ward_' + wardId + '_name_span').text(data.name).show();				
				$('#ward_' + wardId + '_name_input').hide();
				
				$('#ward_' + wardId + '_code_span').text(data.code).show();				
				$('#ward_' + wardId + '_code_input').hide();
				
				$('#ward_' + wardId + '_edit').show();
				$('#ward_' + wardId + '_save').hide();
				$('#ward_' + wardId + '_cancel').hide();			
			},
			"json"
		);
	}
};

function cancelWard(wardId){
	$('#error_ward').html('');
	$('#ward_' + wardId + '_name_span').show();		
	$('#ward_' + wardId + '_name_input').hide();
	
	$('#ward_' + wardId + '_code_span').show();		
	$('#ward_' + wardId + '_code_input').hide();
	
	$('#ward_' + wardId + '_edit').show();
	$('#ward_' + wardId + '_save').hide();
	$('#ward_' + wardId + '_cancel').hide();		
}

function getAllProvincesSelect(divId){
	$('#error_district').html('');
	$('#error_ward').html('');
	$.get(
		'../Province/getAllProvincesSelect?set_ajax_view',
		{
		},
		function(data){
			if(divId == 'district'){
				$('select#select_province1').html('');
				$('select#select_province1').append("<option id='empty' value='empty'>--" + _("Select province") + "--</option>");
				$('select#select_province1').append(data);
			}else{
				$('select#select_province2').html('');
				$('select#select_province2').append("<option id='empty' value='empty'>--" + _("Select province") + "--</option>");
				$('select#select_province2').append(data);
				$('select#select_district').html('');
				$('select#select_district').append("<option id='empty' value='empty'>--" + _("Select district") + "--</option>");
			}
		}
	);
};



function styleTable(table) {
	
}